package com.nf.dao.impl;

import com.nf.dao.UserInfoDao;
import com.nf.entity.UserInfo;
import com.nf.util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class UserInfoDaoImpl implements UserInfoDao {
    @Override
    public UserInfo findUser(String name, String pass) {
        UserInfo user=null;

        Connection conn= DBUtil.getConnection();
        String sql="select * from userInfo where username=? and password=?";

        PreparedStatement pstmt=null;
        ResultSet rst=null;
        try {
            pstmt=conn.prepareStatement(sql);

            pstmt.setString(1,name);
            pstmt.setString(2,pass);

            rst= pstmt.executeQuery();
            if(rst.next()){
                int id=rst.getInt(1);
                String username=rst.getString(2);
                String password=rst.getString(3);
                String phone=rst.getString(4);
                String sex=rst.getString(5);
                String img=rst.getString(6);
                String registerTime=rst.getString(7);
                int status=rst.getInt(8);

                user=new UserInfo(id,username,password,phone,sex,img,registerTime,status);

            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DBUtil.close(rst,pstmt,conn);
        }
        return user;
    }

    @Override
    public UserInfo checkUser(String name) {
        UserInfo user=null;

        Connection conn= DBUtil.getConnection();
        String sql="select * from userInfo where username=?";

        PreparedStatement pstmt=null;
        ResultSet rst=null;
        try {
            pstmt=conn.prepareStatement(sql);

            pstmt.setString(1,name);

            rst= pstmt.executeQuery();
            if(rst.next()){
                int id=rst.getInt(1);
                String username=rst.getString(2);
                String password=rst.getString(3);
                String phone=rst.getString(4);
                String sex=rst.getString(5);
                String img=rst.getString(6);
                String registerTime=rst.getString(7);
                int status=rst.getInt(8);

                user=new UserInfo(id,username,password,phone,sex,img,registerTime,status);

            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DBUtil.close(rst,pstmt,conn);
        }
        return user;
    }

    @Override
    public int add(String name,String pass,String phone) {
        int r=0;

        Connection conn=DBUtil.getConnection();
        String sql="insert into userInfo(username,password,phone) values(?,?,?)";
        PreparedStatement pstmt=null;
        try {
            pstmt=conn.prepareStatement(sql);

            pstmt.setString(1,name);
            pstmt.setString(2,pass);
            pstmt.setString(3,phone);

            r=pstmt.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DBUtil.close(null,pstmt,conn);
        }
        return r;
    }

    @Override
    public int updatePass(int id, String pass) {
        int r=0;

        Connection conn=DBUtil.getConnection();
        String sql="update userInfo set password=? where user_id=?";
        PreparedStatement pstmt=null;
        try {
            pstmt=conn.prepareStatement(sql);
            pstmt.setString(1,pass);
            pstmt.setInt(2,id);

            r= pstmt.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DBUtil.close(null,pstmt,conn);
        }

        return r;
    }

    @Override
    public int updatePerson(String name, String sex, String phone, String img, int id) {
        int r=0;

        Connection conn=DBUtil.getConnection();
        String sql="update userInfo set username=?,phone=?,sex=?,image=? where user_id=?";
        PreparedStatement pstmt=null;
        try {
            pstmt=conn.prepareStatement(sql);
            pstmt.setString(1,name);
            pstmt.setString(2,phone);
            pstmt.setString(3,sex);
            pstmt.setString(4,img);
            pstmt.setInt(5,id);

            r= pstmt.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DBUtil.close(null,pstmt,conn);
        }

        return r;
    }

    @Override
    public List<UserInfo> selectAll() {
        List<UserInfo> list=new ArrayList<>();

        Connection conn=DBUtil.getConnection();
        String sql="select * from userInfo";
        PreparedStatement pstmt=null;
        ResultSet rst=null;
        try {
            pstmt=conn.prepareStatement(sql);
            rst= pstmt.executeQuery();
            while (rst.next()){
                int id=rst.getInt(1);
                String username=rst.getString(2);
                String password=rst.getString(3);
                String phone=rst.getString(4);
                String sex=rst.getString(5);
                String img=rst.getString(6);
                String registerTime=rst.getString(7);
                int status=rst.getInt(8);

                UserInfo user=new UserInfo(id,username,password,phone,sex,img,registerTime,status);
                list.add(user);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DBUtil.close(rst,pstmt,conn);
        }

        return list;

    }

    @Override
    public int UpdateStatus(int userId, int status) {
        int r=0;

        Connection conn=DBUtil.getConnection();
        String sql="update userInfo set status=? where user_id=?";
        PreparedStatement pstmt=null;
        try {
            pstmt=conn.prepareStatement(sql);
            pstmt.setInt(1,status);
            pstmt.setInt(2,userId);
            r=pstmt.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DBUtil.close(null,pstmt,conn);
        }

        return r;
    }

    @Override
    public List<UserInfo> selectByLike(String cond) {
        List<UserInfo> list=new ArrayList<>();

        Connection conn=DBUtil.getConnection();
        String sql="select * from userInfo where username like ? or sex like ?";
        PreparedStatement pstmt=null;
        ResultSet rst=null;
        try {
            pstmt=conn.prepareStatement(sql);
            pstmt.setString(1,"%"+cond+"%");
            pstmt.setString(2,"%"+cond+"%");
            rst= pstmt.executeQuery();
            while (rst.next()){
                int id=rst.getInt(1);
                String username=rst.getString(2);
                String password=rst.getString(3);
                String phone=rst.getString(4);
                String sex=rst.getString(5);
                String img=rst.getString(6);
                String registerTime=rst.getString(7);
                int status=rst.getInt(8);

                UserInfo user=new UserInfo(id,username,password,phone,sex,img,registerTime,status);
                list.add(user);
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DBUtil.close(rst,pstmt,conn);
        }

        return list;
    }
}
